package com.hxzy.yygh.hosp.service.impl;


import com.alibaba.fastjson.JSON;
import com.hxzy.yygh.hosp.repository.DepartmentRepository;
import com.hxzy.yygh.hosp.service.DepartmentService;
import com.hxzy.yygh.model.hosp.Department;
import com.hxzy.yygh.vo.hosp.DepartmentQueryVo;
import com.hxzy.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentRepository departmentRepository;

    @Override
    public void saveDepartment(Map<String, Object> paramMap) {
        //把map集合转换为实体类对象
        String jsonString = JSON.toJSONString(paramMap);
        Department department = JSON.parseObject(jsonString, Department.class);

        //从map集合中获取参数
        String hoscode = department.getHoscode();
        String depcode = department.getDepcode();

        //根据医院编号和科室编号，查询mongoDB数据中，是否存在此数据
        Department departmentExist = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode,depcode);

        //判断该数据在数据库中是否存在
        if (departmentExist == null){
            //执行新增操作
            //封装运维字段
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            //执行新增方法
            departmentRepository.save(department);
        }else{
            //执行修改操作
            //封装运维字段
            department.setCreateTime(departmentExist.getCreateTime());
            department.setUpdateTime(new Date());
            department.setIsDeleted(departmentExist.getIsDeleted());
            //执行修改方法
            departmentRepository.save(department);
        }
    }

    @Override
    public Page<Department> findPageDepartment(int page, int limit, DepartmentQueryVo departmentQueryVo) {
        //1.封装pageAble参数
        Pageable pageAble = PageRequest.of(page - 1, limit);

        //2.封装Department对象
        Department department = new Department();
            //属性拷贝
            BeanUtils.copyProperties(departmentQueryVo, department);
            //属性赋值
            department.setIsDeleted(0);

        //2.封装matcher对象
        ExampleMatcher matcher = ExampleMatcher.matching()
                        .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                                .withIgnoreCase(true);
        //4.封装example对象参数
        Example<Department> example = Example.of(department, matcher);

        //5.执行查询全部方法并返回
        return departmentRepository.findAll(example, pageAble);
    }

    @Override
    public void removeDepartment(String hoscode, String depcode) {
        //根据医院编码和科室编码查询
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department != null){
            //根据id删除
            departmentRepository.deleteById(department.getId());
        }
    }

    @Override
    public List<DepartmentVo> findDeptList(String hoscode) {
        //用于封装最终数据
        List<DepartmentVo> result = new ArrayList<>();

        //根据医院编号，查询医院科室
        Department department = new Department();
        department.setHoscode(hoscode);
        Example<Department> example = Example.of(department);
        List<Department> departmentList = departmentRepository.findAll(example);

        //根据大科室编号分组
        Map<String, List<Department>> departmentMap = departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));

        //遍历逐一处理每一个组
        for (Map.Entry<String, List<Department>> entry : departmentMap.entrySet()){
            //大科室编号
            String bigCode = entry.getKey();

            //大科室编号对应的  ===>  全部的数据
            List<Department> departmentBigList = entry.getValue();

                //封装大科室
                DepartmentVo departmentVo = new DepartmentVo();
                departmentVo.setDepcode(bigCode);
                departmentVo.setDepname(departmentBigList.get(0).getBigname());

                //封装小科室
                List<DepartmentVo> children = new ArrayList<>();
                for (Department dep : departmentBigList){
                    DepartmentVo departmentVo1 = new DepartmentVo();
                    departmentVo1.setDepcode(dep.getDepcode());
                    departmentVo1.setDepname(dep.getDepname());
                    //添加到集合中去
                    children.add(departmentVo1);
                }
                //将小科室添加到大科室
                departmentVo.setChildren(children);
                //将一个处理好的的大科室，返回给result
                result.add(departmentVo);
        }
        return result;
    }

    //根据科室编号，查询对应科室名称
    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department != null) {
            return department.getDepname();
        }
        return null;
    }

    //根据医院编号、科室编号,获取科室对象
    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
    }
}
